package com.zheng.umbrella.web.controller.manage;

import com.zheng.common.base.BaseController;
import com.zheng.common.constant.Constant;
import com.zheng.umbrella.common.constant.UmbrellaResult;
import com.zheng.umbrella.common.constant.UmbrellaResultConstant;
import com.zheng.umbrella.dao.model.UmbrellaHardwareBatch;
import com.zheng.umbrella.dao.model.UmbrellaHardwareBatchExample;
import com.zheng.umbrella.rpc.api.UmbrellaHardwareApiService;
import com.zheng.umbrella.rpc.api.UmbrellaHardwareBatchService;
import com.zheng.upms.dao.model.UpmsUser;
import com.zheng.upms.rpc.api.UpmsApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/9/16.
 */
@Controller
@Api(value = "批次管理", description = "批次管理")
@RequestMapping("/manage/batch")
public class UmbrellaHardwareBatchController  extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UmbrellaHardwareBatchController.class);

    @Autowired
    private UmbrellaHardwareBatchService umbrellaHardwareBatchService;

    @Autowired
    private UmbrellaHardwareApiService umbrellaHardwareApiService;

    @Autowired
    private UpmsApiService upmsApiService;


    @ApiOperation(value = "批次首页")
    @RequiresPermissions("umbrella:batch:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/batch/index.jsp";
    }

    @ApiOperation(value = "批次列表")
    @RequiresPermissions("umbrella:batch:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        String userName = getLoginUserName();
        Integer agentId = upmsApiService.selectUserAgentIdByUsername(userName);

        UmbrellaHardwareBatchExample umbrellaHardwareBatchExample = new UmbrellaHardwareBatchExample();
        UmbrellaHardwareBatchExample.Criteria criteria = umbrellaHardwareBatchExample.createCriteria();
        criteria.andFlagEqualTo(Constant.CONST_FLAG_NORMAL);
        if(agentId!=null){
            criteria.andAgentIdEqualTo(agentId);
        }
        if (!StringUtils.isBlank(sort) && !StringUtils.isBlank(order)) {
            umbrellaHardwareBatchExample.setOrderByClause(sort + " " + order);
        }
        if (StringUtils.isNotBlank(search)) {
            criteria.andCodeLike("%" + search + "%");
        }
        List<UmbrellaHardwareBatch> rows = umbrellaHardwareBatchService.selectByExampleForOffsetPage(umbrellaHardwareBatchExample, offset, limit);
        long total = umbrellaHardwareBatchService.countByExample(umbrellaHardwareBatchExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增批次")
    @RequiresPermissions("umbrella:batch:create")
    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String create(ModelMap modelMap) {
        return "/manage/batch/create.jsp";
    }

    @ApiOperation(value = "新增批次")
    @RequiresPermissions("umbrella:batch:create")
    @ResponseBody
    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    public Object create(UmbrellaHardwareBatch umbrellaHardwareBatch) {
//        ComplexResult result = FluentValidator.checkAll()
//                .on(upmsAgent.getName(), new LengthValidator(1, 120, "名称"))
//                .doValidate()
//                .result(ResultCollectors.toComplex());
//        if (!result.isSuccess()) {
//            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
//        }
        String loginUserName = getLoginUserName();
        long time = System.currentTimeMillis();
        umbrellaHardwareBatch.setAgentId(upmsApiService.selectUserAgentIdByUsername(loginUserName));
        umbrellaHardwareBatch.setCtime(time);
        umbrellaHardwareBatch.setCuser(loginUserName);
        int count = umbrellaHardwareBatchService.insertSelective(umbrellaHardwareBatch);
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "删除批次")
    @RequiresPermissions("umbrella:batch:delete")
    @RequestMapping(value = "/delete/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object delete(@PathVariable("ids") String ids) {
//        int count = umbrellaHardwareBatchService.deleteByPrimaryKeys(ids);
        int count = umbrellaHardwareBatchService.deleteLogicByPrimaryKeys(ids,UmbrellaHardwareBatch.class,null);
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "修改批次")
    @RequiresPermissions("umbrella:batch:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.GET})
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        UmbrellaHardwareBatch umbrellaHardwareBatch = umbrellaHardwareBatchService.selectByPrimaryKey(id);
        modelMap.put("umbrellaHardwareBatch", umbrellaHardwareBatch);
        return "/manage/batch/update.jsp";
    }

    @ApiOperation(value = "修改批次")
    @RequiresPermissions("umbrella:batch:update")
    @RequestMapping(value = "/update/{id}", method = {RequestMethod.POST})
    @ResponseBody
    public Object update(@PathVariable("id") int id, UmbrellaHardwareBatch umbrellaHardwareBatch) {
//        ComplexResult result = FluentValidator.checkAll()
//                .on(upmsAgent.getName(), new LengthValidator(1, 20, "名称"))
//                .doValidate()
//                .result(ResultCollectors.toComplex());
//        if (!result.isSuccess()) {
//            return new UpmsResult(UpmsResultConstant.INVALID_LENGTH, result.getErrors());
//        }
        umbrellaHardwareBatch.setId(id);
        int count = umbrellaHardwareBatchService.updateByPrimaryKeySelective(umbrellaHardwareBatch);
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }

}
